require 'openssl'
require 'digest'
module SignService
	def self.create_sign()
		project_users = ProjectUser.where("join_time IS NOT NULL AND exit_time IS NULL and role = 3")
		yest = Time.now - 86400
		yest_year = yest.year
		yest_month = yest.month
		yest_day = yest.day
		yest_date = CommonApi::format_time_new(yest)
		project_users.each do |pu|
			sign = Sign.where(user_id: pu.user_id, project_id: pu.project_id, year: yest_year, month: yest_month, day: yest_day)
			sign_supplementaries = 	SignSupplementary.where("project_user_id = ? and supplementary_date =? ", pu.id, yest_date)
			if sign.length == 1 && sign_supplementaries.empty?
				if sign.first.create_at = sign.first.update_at
					if sign.first.create_at.strftime("%H:%M:%S") < "12:00:00"
						sign_supplementary = SignSupplementary.new
						sign_supplementary.project_user_id = pu.id
						user = User.find(pu.user_id)
						user_name = user.name.nil? ? "" : user.name
						sign_supplementary.supplementary_content = "员工：" + user_name + " 申请补卡，" + "补卡时间为：" + yest.strftime("%Y-%m-%d") + "补卡类型" + "上班卡"
						sign_supplementary.supplementary_date = yest.strftime("%Y-%m-%d")
						sign_supplementary.supplementary_status = 1
						sign_supplementary.project_id = pu.project_id
						sign_supplementary.suppl_type = 1
						sign_supplementary.save
					else
						sign_supplementary = SignSupplementary.new
						sign_supplementary.project_user_id = pu.id
						user = User.find(pu.user_id)
						user_name = user.name.nil? ? "" : user.name
						sign_supplementary.supplementary_content = "员工：" + user_name + " 申请补卡，" + "补卡时间为：" + yest.strftime("%Y-%m-%d") + "补卡类型" + "上班卡"
						sign_supplementary.supplementary_date = yest.strftime("%Y-%m-%d")
						sign_supplementary.supplementary_status = 1
						sign_supplementary.project_id = pu.project_id
						sign_supplementary.suppl_type = 2
						sign_supplementary.save
					end
				end	
			elsif sign.length == 0 && sign_supplementaries.empty?
				sign_supplementary = SignSupplementary.new
				sign_supplementary.project_user_id = pu.id
				user = User.find(pu.user_id)
				user_name = user.name.nil? ? "" : user.name
				sign_supplementary.supplementary_content = "员工：" + user_name + " 申请补卡，" + "补卡时间为：" + yest.strftime("%Y-%m-%d") + "补卡类型" + "全天卡"
				sign_supplementary.supplementary_date = yest.strftime("%Y-%m-%d")
				sign_supplementary.supplementary_status = 1
				sign_supplementary.project_id = pu.project_id
				sign_supplementary.suppl_type = 3
				sign_supplementary.save
			end
		end
	end
end